﻿using LevelEditor.Model.Entity;
using LevelEditor.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LevelEditor.View.UserControls
{
    /// <summary>
    /// Interaction logic for GameElementsPanel.xaml
    /// </summary>
    public partial class GameElementsPanel : UserControl
    {
    // Fields
    private Image currentSelectedSprite;

    private List<GameSprite> gameSprites;
    private Dictionary<string, WrapPanel> wrapPanelsDictionnary;

    // Events
    public event EventHandler SpriteBrushSelected;

    // Methods
    public GameElementsPanel()
    {
        this.InitializeComponent();
    }

    protected void OnSpriteBrushSelected(object sender)
    {
        if (this.SpriteBrushSelected != null)
        {
            this.SpriteBrushSelected(sender, new EventArgs());
        }
    }

    protected void sprite_MouseEnter(object sender, MouseEventArgs e)
    {
        Image image = (Image) sender;
        GameSprite dataContext = (GameSprite) image.DataContext;
        Border border = (Border) image.Parent;
        border.Background = Brushes.LightGray;
    }

    protected void sprite_MouseLeave(object sender, MouseEventArgs e)
    {
        Image image = (Image) sender;
        GameSprite dataContext = (GameSprite) image.DataContext;
        Border border = (Border) image.Parent;
        border.Background = Brushes.Transparent;
    }

    protected void sprite_MouseUp(object sender, MouseButtonEventArgs e)
    {
        Image image = (Image) sender;
        Border border = (Border) image.Parent;
        border.BorderBrush = Brushes.Red;
        GameSprite model = (GameSprite) image.DataContext;
        model.IsSelected = true;
        if (this.currentSelectedSprite != null)
        {
            ((GameSprite) this.currentSelectedSprite.DataContext).IsSelected = false;
            Border currSelectBorder = (Border) this.currentSelectedSprite.Parent;
            currSelectBorder.BorderBrush = Brushes.Transparent;
        }
        this.currentSelectedSprite = image;
        this.OnSpriteBrushSelected(this.currentSelectedSprite);
    }

    // Properties
    public Image CurrentSelectedSprite
    {
        get
        {
            return this.currentSelectedSprite;
        }
    }

    public List<GameSprite> GameSprites
    {
        set
        {
            this.gameSprites = value;
            string appPath = FileUtil.AppStartUpPath;
            this.spritesList.Children.Clear();
            this.wrapPanelsDictionnary = new Dictionary<string, WrapPanel>();
            for (int i = 0; i < this.gameSprites.Count; i++)
            {
                GameSprite item = this.gameSprites.ElementAt<GameSprite>(i);
                if (!this.wrapPanelsDictionnary.Keys.Contains<string>(item.Category))
                {
                    Expander expander = new Expander {
                        Header = item.Category
                    };
                    WrapPanel wrapPanel = new WrapPanel();
                    expander.Content = wrapPanel;
                    this.spritesList.Children.Add(expander);
                    this.wrapPanelsDictionnary.Add(item.Category, wrapPanel);
                }
                MemoryStream ms = new MemoryStream();
                FileStream stream = new FileStream(appPath + item.ImageSource.OriginalString, FileMode.Open, FileAccess.Read);
                ms.SetLength(stream.Length);
                stream.Read(ms.GetBuffer(), 0, (int) stream.Length);
                ms.Flush();
                stream.Close();
                BitmapImage img = new BitmapImage();
                img.BeginInit();
                img.StreamSource = ms;
                img.CacheOption = BitmapCacheOption.OnLoad;
                img.EndInit();
                Image image = new Image {
                    Source = img,
                    Width = img.PixelWidth,
                    Height = img.PixelHeight,
                    MaxWidth = 35.0,
                    MaxHeight = 35.0,
                    Margin = new Thickness(5.0, 5.0, 5.0, 5.0),
                    SnapsToDevicePixels = true
                };
                image.MouseUp += new MouseButtonEventHandler(this.sprite_MouseUp);
                image.MouseEnter += new MouseEventHandler(this.sprite_MouseEnter);
                image.MouseLeave += new MouseEventHandler(this.sprite_MouseLeave);
                image.DataContext = item;
                Border border = new Border {
                    BorderThickness = new Thickness(1.0),
                    Child = image
                };
                this.wrapPanelsDictionnary[item.Category].Children.Add(border);
            }
        }
    }
}


}
